﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;

namespace GK320_Helper
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            string str0 = "adb devices";

            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(str0 + "&exit");
            p.StandardInput.AutoFlush = true;
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            if (output.IndexOf("unauthorized") != -1)
                System.Windows.MessageBox.Show("请打开电子学生证确认对话框", "安装提示");
            if (output.IndexOf("0123456789ABCDEF") != -1 && output.IndexOf("unauthorized") == -1)
            {
                System.Windows.MessageBox.Show("检查设备完成！可以进入下一步操作", "完成");
                Button2.IsEnabled = true;
            }
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            string apkname = ApkName.Text;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine("adb push mtk-su /data/local/tmp" + "\n" + "adb push "+ApkName.Text+" /sdcard/"+"\n"+"echo cd /data/local/tmp > temp.txt"+"\n"+"echo chmod 755 mtk-su >> temp.txt"+"\n"+"echo ./mtk-su >> temp.txt"+"\n"+"echo mount -o remount /system >> temp.txt"+"\n"+"echo cd /system/app >> temp.txt"+"\n"+"echo mkdir "+ApkFolder.Text+" >> temp.txt"+"\n"+"echo cp /sdcard/"+ApkName.Text+" /system/app/"+ApkFolder.Text+"/ >> temp.txt"+"\n"+"echo chmod 644 /system/app/"+ApkFolder.Text+"/"+ApkName.Text+" >> temp.txt"+"\n"+"echo exit >> temp.txt"+"\n"+"echo exit >> temp.txt"+"\n"+"adb shell < temp.txt"+"&exit");
            p.StandardInput.AutoFlush = true;
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            if (ToogleSwitch1.IsEnabled == true)
            {
                p.Start();
                p.StandardInput.WriteLine("adb reboot" + "&exit");
                p.StandardInput.AutoFlush = true;
                output = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
                p.Close();
            }
               
        }
    }
}
